import { Commit, StoreOptions } from 'vuex';
import LocalCache, { setToken, getToken, setTokenToName } from '@/utils/auth';
import { getParamsURL } from '@/utils/Object'

const user = {
    namespaced: true,
    state: {
        // 登录用户信息
        userInfo: '',
        // 团队分组
        teamInfo: '',
        // 坐席信息 备注 姓名  班次  typeClass
        userAssociate: '',

        // 工作流登录信息
        workFlowInfo: {
            loginFlag: false, // 是否已登录 false - 未登录(或登录失败)
            unFinished: 0, // 未完成
            canReceive: 0, // 可领取
            companyId: '', // 银行号
            userId: '', // 登录用户工号
            baseUrl: '', // 工作流系统地址前缀
        },

        // 银行号
        bankNo: '',
        serviceData: {}, // 服务历史数据
        // 动态获取 字典里的 工单系统URL后缀路径
        flowserverBackendUrl: '',
        // 工作流工单编号连接地址
        flowserverBackendAUrl: '',
    },

    mutations: {
        /**
             * 更新 服务历史数据
             * @param state
             * @param data 最新服务历史数据
             */
        UPDATE_SERVICE_DATA(state: any, data: any) {
            state.serviceData = data;
        },
        /**
         * 更新 登录用户信息
         * @param state
         * @param userInfo 登录用户信息
         */
        UPDATE_USER_INFO(state: any, userInfo: any) {
            const cookileval = JSON.stringify(userInfo);
            setToken(cookileval);
            // console.log('cookileval_____', cookileval);

            setTokenToName(cookileval);
            // localStorage.setItem('userInfo', JSON.stringify(userInfo));
            state.userInfo = userInfo;
        },

        /**
         * 更新 工作流登录信息
         * @param state
         * @param workFlowInfo 工作流登录信息
         */
        UPDATE_WORKFLOW_INFO(state: any, workFlowInfo: any) {
            state.workFlowInfo = workFlowInfo;
        },
        // 获取当前用户信息
        GET_USER_INFO(state: any) {
            if (state.userInfo === '') {
                state.userInfo = getToken() ? JSON.parse(getToken() || '') : '';
            }
        },

        /**
             * 更新 --卡系统-- 默认请求报文信息
             * @param state
             * @param teamInfo 最新请求报文信息
        */
        UPDATE_TEAMTNFO(state: any, teamInfo: any) {
            state.teamInfo = teamInfo;
        },
        // 
        UPDATE_USERASSOCIATE(state: any, userAssociate: any) {
            state.userAssociate = userAssociate;
        },

        DELECT_USER_INFOR(state: any, teamInfo: any) {
            state.teamInfo = '';
            state.userInfo = '';
            state.userAssociate = '';
        },
        // 更新 flowserverBackendUrl 字段的值
        UPDATE_FLOWSERVER_RUL(state: any, flowserverBackendUrl: string) {
            state.flowserverBackendUrl = flowserverBackendUrl;
        },
        // 更新 flowserverBackendUrl 字段的值
        UPDATE_FLOWSERVER_A_RUL(state: any, flowserverBackendAUrl: string) {
            state.flowserverBackendAUrl = flowserverBackendAUrl;
        }

    },

    actions: {
        delStateUserinforAll: (context: { commit: Commit, state: any }) => {
            context.commit('DELECT_USER_INFOR');
        },
        // 获取工作流的URl 的后缀路径
        async updatedFlowserverBackendUrl(context: { commit: Commit, state: any }) {
            const flowserverBackendUrl = await getParamsURL('flowserver_backend_addr_url');

            context.commit('UPDATE_FLOWSERVER_RUL', flowserverBackendUrl)
            // 设置缓存
            LocalCache.setCache('flowserverBackendUrl', flowserverBackendUrl)


            // 工作流工单编号连接地址
            const flowserverBackendAUrl = await getParamsURL('flowserver_backend_a_url');
            // 设置缓存
            context.commit('UPDATE_FLOWSERVER_A_RUL', flowserverBackendAUrl)
            LocalCache.setCache('flowserverBackendAUrl', flowserverBackendAUrl)

        },
        loadLocalLongin(context: { commit: Commit, state: any }) {
            // 开始从缓存 localStorage 取数据 有就用没有就不管
            const flowserverBackendUrl = LocalCache.getCache('flowserverBackendUrl');
            if (flowserverBackendUrl) {
                context.commit('UPDATE_FLOWSERVER_RUL', flowserverBackendUrl)
            }
            // 工作流工单编号连接地址
            const flowserverBackendAUrl = LocalCache.getCache('flowserverBackendAUrl');
            if (flowserverBackendAUrl) {
                context.commit('UPDATE_FLOWSERVER_A_RUL', flowserverBackendAUrl)
            }
        }
    },

};

export default user;
